package com.mz.ozy.adapter;

import android.Manifest;
import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.mz.ozy.R;
import com.mz.ozy.bean.MyOrderInfoBean;
import com.mz.ozy.utils.FullyGridLayoutManager;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.tbruyelle.rxpermissions.RxPermissions;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import rx.functions.Action1;

/**
 * Created by Administrator on 2018/11/23.
 */

public class EvauInfoAdapter extends BaseQuickAdapter<MyOrderInfoBean.OrdermapBean.ProdcutListBean, BaseViewHolder> {
    private final Activity mActivity;
    private int maxSelectNum = 9;
    private List<List<String>> mAdapterImgArr = new ArrayList<>();
    private EvauInfoAdapter.onAddPicClickListener mOnAddPicClickListener;

    public EvauInfoAdapter(Activity activity, @Nullable List<MyOrderInfoBean.OrdermapBean.ProdcutListBean> data) {
        super(R.layout.view_item_my_evau, data);
        this.mActivity = activity;
        for (int i = 0; i < data.size(); i++) {
            mAdapterImgArr.add(new ArrayList<>());
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, MyOrderInfoBean.OrdermapBean.ProdcutListBean item) {
        RadioGroup radioGroup = helper.getView(R.id.pay_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                View checkedView = group.findViewById(checkedId);
                int tag = Integer.valueOf(checkedView.getTag().toString());

                mOnAddPicClickListener.setSelectedNum(tag, helper.getAdapterPosition());
            }
        });
        EditText et_evaluate = helper.getView(R.id.et_evaluate);
        mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        et_evaluate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                mOnAddPicClickListener.setSelectedNum1(s.toString(), helper.getAdapterPosition());
            }
        });

        helper.addOnClickListener(R.id.btn_refund);
        ImageView ivIcon = helper.getView(R.id.iv_icon);
        RecyclerView mRvPhoto = helper.getView(R.id.rv_photo);
        helper.setText(R.id.tv_title, item.getProduct_name())
                .setText(R.id.tv_info, "null".equals(item.getArrribute_name()) ? "" : item.getArrribute_name())
                .setText(R.id.tv_count, "×" + item.getNum())
                .setText(R.id.tv_money, getPrice(item));
        final int adapterPosition = helper.getAdapterPosition();
        EvaluatePhotoAdapter mAdapter = new EvaluatePhotoAdapter(mAdapterImgArr.get(adapterPosition), R.mipmap.post_msg_plus_pic);
        mAdapter.setOnAddPicClickListener(new ReleaseMsgAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                mOnAddPicClickListener.onAddPicClick(adapterPosition);
                RxPermissions.getInstance(mActivity)
                        .request(Manifest.permission.CAMERA)//这里填写所需要的权限
                        .subscribe(new Action1<Boolean>() {
                            @Override
                            public void call(Boolean aBoolean) {
                                if (aBoolean) {//true表示获取权限成功（注意这里在android6.0以下默认为true）
                                    PictureSelector.create((Activity) mActivity)
                                            .openGallery(PictureMimeType.ofImage())
                                            .maxSelectNum(maxSelectNum - mAdapter.getData().size())
                                            .minSelectNum(1)
                                            .imageSpanCount(4)
                                            .selectionMode(PictureConfig.MULTIPLE)
                                            .forResult(PictureConfig.CHOOSE_REQUEST);
                                } else {
                                    Log.i("permissions", Manifest.permission.READ_EXTERNAL_STORAGE + "：" + "获取失败");
                                }
                            }
                        });

            }

            @Override
            public void onRemovePicClick(int index) {
                mOnAddPicClickListener.onRemovePicClick(adapterPosition, index);

//                img.remove(index);
            }
        });
        mRvPhoto.setHasFixedSize(true);
        FullyGridLayoutManager gridLayoutManager = new FullyGridLayoutManager(mActivity, 4, GridLayoutManager.VERTICAL, false);
        mRvPhoto.setLayoutManager(gridLayoutManager);
        mAdapter.setSelectMax(maxSelectNum);
        mRvPhoto.setAdapter(mAdapter);
        String img_zt = "";
        if (!TextUtils.isEmpty(item.getImg_zt())) {
            final String[] splitImgZt = item.getImg_zt().split(",");
            if (splitImgZt.length > 0) {
                img_zt = splitImgZt[0];
            }
        }
        GlideUtils.loadRoundImg(mContext, ivIcon, img_zt, R.mipmap.default_foods_icon, 4);
    }

    public void setOnAddPicClickListener(EvauInfoAdapter.onAddPicClickListener onAddPicClickListener) {
        mOnAddPicClickListener = onAddPicClickListener;
    }

    public void addAdapterImg(int position, String path) {
        final List<String> imgArr = mAdapterImgArr.get(position);
        imgArr.add(path);
        notifyDataSetChanged();
    }

    public void removeAdapterImg(int position, int index) {
        final List<String> imgArr = mAdapterImgArr.get(position);
        imgArr.remove(index);
        notifyDataSetChanged();
    }

    public interface onAddPicClickListener {
        void onAddPicClick(int adapterPosition);

        void onRemovePicClick(int adapterPosition, int index);

        void setSelectedNum(int tag, int num);

        void setSelectedNum1(String num, int adapterPosition);


    }

    @NotNull
    private String getPrice(MyOrderInfoBean.OrdermapBean.ProdcutListBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getPrice()) && Double.parseDouble(item.getPrice()) != 0.00) {
            price += item.getPrice();
            price += "KT";
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += item.getMarket_price();
                price += "消费积分";
            } else {
                price += item.getMarket_price();
                price += "消费积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "0.00KT";
        }
        return price;
    }

}
